package com.genesis.yunnanji.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.genesis.yunnanji.R;
import com.youth.xframe.widget.XStateButton;
import com.youth.xframe.widget.XToast;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/28 9:32
 * 作者：Kite
 * 描述：绑定新卡
 */
public class BindingCardActivity extends BaseActivity {

    @BindView(R.id.et_code_num)
    EditText etCodeNum;

    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.btn_binding)
    XStateButton btnBinding;

    @BindView(R.id.btn_check)
    XStateButton btnCheck;

    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_binding_card;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("绑定新卡");
        editIsEmpty();
    }

    @OnClick({R.id.btn_binding, R.id.btn_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_binding:
                XToast.info("123");
                break;
            case R.id.btn_check:
                XToast.info("321");
                break;
        }
    }

    private void editIsEmpty() {
        TextWatcher codeNumWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String codeNum = editable.toString().replace(" ", "");
                    if (!TextUtils.isEmpty(codeNum)) {
                        errorMap.remove(1);
                        if (codeNum.length() == 15) {
                            errorMap.remove(2);
                        } else {
                            errorMap.put(2, "请填写正确的卡号");
                        }
                    } else {
                        errorMap.put(1, "请输入您的卡号");
                    }
                    changeClickable();
                }
            }
        };
        etCodeNum.addTextChangedListener(codeNumWatcher);

        TextWatcher passwordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String password = editable.toString();
                    if (!TextUtils.isEmpty(password)) {
                        errorMap.remove(3);
                        if (password.length() ==8) {
                            errorMap.remove(4);
                        } else {
                            errorMap.put(4, "请输入正确的密码");
                        }
                    } else {
                        errorMap.put(3, "请输入密码");
                    }
                    changeClickable();
                }
            }
        };
        etPassword.addTextChangedListener(passwordWatcher);
        errorMap.put(4, "请输入您的卡号");
        errorMap.put(1, "请输入您的密码");

        btnBinding.setClickable(false);
        btnCheck.setClickable(false);
        btnBinding.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
        btnCheck.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnBinding.setClickable(true);
            btnCheck.setClickable(true);
            btnBinding.setNormalBackgroundColor(getResources().getColor(R.color.colorTheme));
            btnCheck.setNormalBackgroundColor(getResources().getColor(R.color.colorTheme));
        } else {
            btnBinding.setClickable(false);
            btnCheck.setClickable(false);
            btnBinding.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
            btnCheck.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
        }
    }

}
